// pages/form/form.js
import tool from '../../modules/tool'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    data: {},
    show: false,
    list: {}
  },

  handleImageSelectorChange(e) {
    let dataset = e.currentTarget.dataset
    let value = e.detail

    try {
      this.data.list.list[dataset.index].config.onChange(value)
    } catch (error) {

    }
  },

  handleListInput(e) {
    let dataset = e.currentTarget.dataset
    let value = e.detail.value
    try {
      this.data.list.list[dataset.index].config.onInput(value)
    } catch (error) {

    }
  },

  handleTopBottomButtonClick(e) {
    let dataset = e.currentTarget.dataset
    if (dataset.position == 'top') {
      try {
        this.data.list.top.list[dataset.index].config.onClick()
      } catch (error) {

      }
    }
    if (dataset.position == 'bottom') {
      try {
        this.data.list.bottom.list[dataset.index].config.onClick()
      } catch (error) {

      }
    }
  },

  handleToBottom() {
    try {
      this.data.list.toBottomCallBack()
    } catch (error) {

    }
  },

  handlePatrolListMore(e) {
    let dataset = e.currentTarget.dataset
    let value = e.detail
    try {
      this.data.list.list[dataset.index].config.moreCallBack(value)
    } catch (error) {

    }
  },

  handlePatrolListOpen(e) {
    let dataset = e.currentTarget.dataset
    let value = e.detail
    try {
      this.data.list.list[dataset.index].config.openCallBack(value)
    } catch (error) {

    }
  },

  handleTopBottomInput(e) {
    let dataset = e.currentTarget.dataset
    let value = e.detail.value
    if (dataset.position == 'top') {
      try {
        this.data.list.top.list[dataset.index].config.onInput(value)
      } catch (error) {
        console.log(this.data.list)
      }
    }
    if (dataset.position == 'bottom') {
      try {
        this.data.list.bottom.list[dataset.index].config.onInput(value)
      } catch (error) {

      }
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    tool.setPageData(this)
    let data = tool.store.getItem('controlFormPage')
    if (!data) {
      wx.navigateBack()
      return
    }
    wx.setNavigationBarTitle({
      title: data.title,
    })
    this.setData({
      data
    })

    this.handleRefreshPage()
    this.setData({
      show: true
    })

  },

  async handleRefreshPage() {
    try {
      let re = await this.data.data.init(async () => {
        try {
          await this.handleRefreshPage()
        } catch (error) {
          console.log(error)
        }
      }, () => { this.handleToTop() })
      console.log(re)
      this.setData({
        list: re
      })
    } catch (error) {
      console.log(error)
    }
  },

  async handleToTop() {
    let t = this.data.list
    this.setData({
      list: []
    })
    await tool.wait(0.001)
    this.setData({
      list: t
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})